﻿using System;
using System.Linq;
using Microsoft.SharePoint;
using SharePoint.Orm.Metal.Model;

namespace SharePoint.Orm.Metal {
	public class DefaultFieldTypeResolver : IFieldTypeResolver {
		public virtual Type GetFieldType(SiteInfo siteInfo, SPField field) {
			switch (field.Type) {
				case SPFieldType.Text:
					return typeof(string);
				case SPFieldType.DateTime:
					return typeof(DateTime?);
				case SPFieldType.Note:
					return typeof(string);
				case SPFieldType.Integer:
					return typeof(int?);
				case SPFieldType.Guid:
					return typeof(Guid?);
				case SPFieldType.Boolean:
					return typeof(bool?);
				case SPFieldType.Choice:
					return typeof(string);
				case SPFieldType.File:
					return typeof(string);
				case SPFieldType.Computed:
					return typeof(string);
				case SPFieldType.User:
					return typeof(SPFieldUserValue);
				case SPFieldType.Invalid:
					return typeof(string);
				case SPFieldType.WorkflowStatus:
					return typeof(string);
				case SPFieldType.URL:
					return typeof(SPFieldUrlValue);
				case SPFieldType.AllDayEvent:
					return typeof(string);
				case SPFieldType.Recurrence:
					return typeof(string);
				case SPFieldType.CrossProjectLink:
					return typeof(string);
				case SPFieldType.Number:
					return typeof(decimal?);
				case SPFieldType.WorkflowEventType:
					return typeof(string);
				case SPFieldType.Lookup:
					return typeof(SPFieldLookupValue);
				case SPFieldType.MultiChoice:
					return typeof(string);
				case SPFieldType.ContentTypeId:
					return typeof (Guid?);
				case SPFieldType.GridChoice:
					return typeof(string);
				case SPFieldType.Counter:
					return typeof(string);
				case SPFieldType.ModStat:
					return typeof(string);
			}
			throw new SPOrmException("Could not find conversion for type '" + field.Type + "'");
		}

		public virtual string GetLookupFieldType(SiteInfo siteInfo, SPFieldLookup field) {
			ListInfo listInfo = siteInfo.Lists.FirstOrDefault(l => new Guid(l.ListId) == new Guid(field.LookupList));
			if (listInfo != null) {
				return listInfo.FullClassName;
			}
			return "object";
		}
	}
}
